package com.xzx.mr.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/22 22:10
 */
public class MyFilterRecordWriter extends RecordWriter<Text, NullWritable> {
    private FSDataOutputStream outputStream1;
    private FSDataOutputStream outputStream2;

    public MyFilterRecordWriter(TaskAttemptContext job) {
        Configuration configuration = job.getConfiguration();
        try {
            FileSystem fileSystem = FileSystem.get(configuration);
            outputStream1 = fileSystem.create(new Path("d:/1.txt"), true);
            outputStream2 = fileSystem.create(new Path("d:/2.txt"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        if (key.toString().contains("atguigu")) {
            outputStream1.write(key.toString().getBytes());
            outputStream1.write("\r\n".getBytes());
        } else {
            outputStream2.write(key.toString().getBytes());
            outputStream2.write("\r\n".getBytes());
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(outputStream1);
        IOUtils.closeStream(outputStream2);
    }
}
